package com.vil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Park;
import com.vil.entity.Pet;
import com.vil.mapper.ParkMapper;
import com.vil.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ParkServiceImpl implements ParkService {
    @Autowired
    private ParkMapper mapper;

    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        try {
            int i = mapper.deleteByPrimaryKey(id);

            if (i > 0) {
                return ServerResponse.buildSuccessMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse getById(Long id) {
        Park park= null;
        try {
            park = mapper.selectByPrimaryKey(id);
            if (park==null){
                return ServerResponse.buildErrorMsg("不存在该管理员信息");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildSuccessData(park);
    }

    @Override
    public ServerResponse insertSelective(Park record) {
        record.setCreate_time(new Date());

        try {
            int i = mapper.insertSelective(record);
            if (i > 0) {
                return ServerResponse.buildSuccessMsg("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("新增失败");
    }

    @Override
    public ServerResponse updateByPrimaryKeySelective(Park park) {

        try {
            int i = mapper.updateByPrimaryKeySelective(park);
            if (i > 0) {
                return ServerResponse.buildSuccessMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("修改失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize,Park park) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Park> list = null;
        try {
            list = mapper.getListByManyConditions(park);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo parkPageInfo = new PageInfo(list);
        return ServerResponse.buildSuccessData(parkPageInfo);
    }

    @Override
    public ServerResponse deleteMany(Long[] ids) {
        try {
            if (ids == null) {
                return ServerResponse.buildErrorMsg("数组不能为空");
            }
            int i = 0;
            i = mapper.deleteMany(ids);
            if (i > 0) {
                return ServerResponse.buildSuccessMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }
}
